/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jun 16, 2012  11:26:28 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.sales
 * File Name : MktRequestDtlAdjustmentServices.java
 * TODO
 */
package jpa.service.promis.sales;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.sales.BusinessPartner;
import jpa.entitys.promis.sales.MktRequestDtlAdjustment;
import jpa.entitys.promis.sales.MktRequestDtlAdjustmentPK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class MktRequestDtlAdjustmentServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(MktRequestDtlAdjustmentServices.class);

	/**
	 * @param em
	 */
	public MktRequestDtlAdjustmentServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	

	public MktRequestDtlAdjustment findById(MktRequestDtlAdjustmentPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "MktRequestDtlAdjustment.findById"));
	}
	
	public MktRequestDtlAdjustment findOldUnit(String company, String project, 
																		String receiptType, String receiptSeries, long receiptNumber, 
																		String paymentRefType, String paymentRefSeries, long paymentRefNumber, 
																		String buildingPhaseCode, String floorZoneCode, String unitNumber, 
																		String status) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company); 									//log.info(" company : " + company);
		mapWhereValue.put("project", project);									//log.info(" project : " + project);
		mapWhereValue.put("receiptType", receiptType);							//log.info(" receiptType : " + receiptType);
		mapWhereValue.put("receiptSeries", receiptSeries);						//log.info(" receiptSeries : " + receiptSeries);
		mapWhereValue.put("receiptNumber", receiptNumber);						//log.info(" receiptNumber : " + receiptNumber);
		mapWhereValue.put("paymentRefType", paymentRefType);					//log.info(" paymentRefType : " + paymentRefType);
		mapWhereValue.put("paymentRefSeries", paymentRefSeries);				//log.info(" paymentRefSeries : " + paymentRefSeries);
		mapWhereValue.put("paymentRefNumber", paymentRefNumber);				//log.info(" paymentRefNumber : " + paymentRefNumber);
		mapWhereValue.put("buildingPhaseCode", buildingPhaseCode);				//log.info(" buildingPhaseCode : " + buildingPhaseCode);
		mapWhereValue.put("floorZoneCode", floorZoneCode);						//log.info(" floorZoneCode : " + floorZoneCode);
		mapWhereValue.put("unitNumber", unitNumber);							//log.info(" unitNumber : " + unitNumber);
		mapWhereValue.put("status", status);									//log.info(" status : " + status);
		
		List<MktRequestDtlAdjustment> lst = super.ConvertToYoureObject(super.find("MktRequestDtlAdjustment.findOldUnit", mapWhereValue));
		if(!CommonService.IsNull(lst))
			return lst.get(0);
		else 
			return null;
	}
	
	public List<MktRequestDtlAdjustment> findOldUnitByCN(String company, String project, 
			String receiptType, String receiptSeries, long receiptNumber, 
			String paymentRefType, String paymentRefSeries, long paymentRefNumber, 
			String status) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company); 									//log.info(" company : " + company);
		mapWhereValue.put("project", project);									//log.info(" project : " + project);
		mapWhereValue.put("receiptType", receiptType);							//log.info(" receiptType : " + receiptType);
		mapWhereValue.put("receiptSeries", receiptSeries);						//log.info(" receiptSeries : " + receiptSeries);
		mapWhereValue.put("receiptNumber", receiptNumber);						//log.info(" receiptNumber : " + receiptNumber);
		mapWhereValue.put("paymentRefType", paymentRefType);					//log.info(" paymentRefType : " + paymentRefType);
		mapWhereValue.put("paymentRefSeries", paymentRefSeries);				//log.info(" paymentRefSeries : " + paymentRefSeries);
		mapWhereValue.put("paymentRefNumber", paymentRefNumber);				//log.info(" paymentRefNumber : " + paymentRefNumber);		
		mapWhereValue.put("status", status);									//log.info(" status : " + status);
		

		return super.ConvertToYoureObject(super.find("MktRequestDtlAdjustment.findOldUnitByCN", mapWhereValue));
		
	}
	
	
	
	public MktRequestDtlAdjustment InsertedOrUpdated(MktRequestDtlAdjustment vlaue) throws Exception {
		MktRequestDtlAdjustment obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{					
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public MktRequestDtlAdjustment Updated(MktRequestDtlAdjustment vlaue) throws Exception {
		MktRequestDtlAdjustment obj = vlaue;
		try{
			
			if(vlaue.getId() != null)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}				
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<MktRequestDtlAdjustment> Inserted(List<MktRequestDtlAdjustment> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MktRequestDtlAdjustment obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<MktRequestDtlAdjustment> Updated(List<MktRequestDtlAdjustment> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(MktRequestDtlAdjustment obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getId()))
				{					
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);					
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}


}
